﻿using System.Collections.Generic;
using UniFramework.Actor;
using UniFramework.Battle;
using UniFramework.Buff;
using UnityEngine;

namespace UniFramework.Battle
{
	///<summary>
	///buff的效果
	///</summary>
	public class BuffData
	{
		public static Dictionary<string, BuffOnOccur> onOccurFunc = new Dictionary<string, BuffOnOccur>()
		{
			{ "OnOccurFunc", OnOccurFunc },
			{ "OnNotCanInputBuff", OnNotCanInputBuffOccur },
			{ "OnExplosionBuff", OnExplosionBuff },
		};

		public static Dictionary<string, BuffOnRemoved> onRemovedFunc = new Dictionary<string, BuffOnRemoved>()
		{
			{ "OnNotCanInputBuffEnd", OnNotCanInputBuffEnd },
			{ "OnExplosionBuffRemoved", OnExplosionBuffRemoved },
		};

		public static Dictionary<string, BuffOnTick> onTickFunc = new Dictionary<string, BuffOnTick>()
		{
			{ "OnTickedFunc", OnTickedFunc },
			{ "OnCheckBullet", OnCheckBullet },
		};

		public static Dictionary<string, BuffOnCast> onCastFunc = new Dictionary<string, BuffOnCast>()
		{
		};

		public static Dictionary<string, BuffOnHit> onHitFunc = new Dictionary<string, BuffOnHit>()
		{
		};

		public static Dictionary<string, BuffOnBeHurt> beHurtFunc = new Dictionary<string, BuffOnBeHurt>()
		{
			{ "OnBeHit", OnBeHit }
		};

		public static Dictionary<string, BuffOnKill> onKillFunc = new Dictionary<string, BuffOnKill>()
		{
		};

		public static Dictionary<string, BuffOnBeKilled> onBeKilledFunc = new Dictionary<string, BuffOnBeKilled>()
		{
			{ "OnBeKilled", OnBeKilled },
			{ "OnActorBeKilled", OnActorBeKilled },
		};

		private static void OnOccurFunc(BuffObj buff, int modifyStack)
		{
			Debug.Log("OnOccurFunc Test");
		}

		/// <summary>
		/// 在移除时销毁当前的对象，
		/// 并给下一个对象添加一个buff
		/// </summary>
		/// <param name="buff"></param>
		/// <param name="modifyStack"></param>
		private static void OnExplosionBuff(BuffObj buff, int modifyStack)
		{
			Debug.Log("OnExplosionBuff");
		}

		private static void OnNotCanInputBuffOccur(BuffObj buff, int modifyStack)
		{
			Debug.Log("玩家关闭输入");
			LogicActor buffTarget = buff.target;
			var logicActorState = buffTarget.GetComponent<LogicActorState>();
			logicActorState.state.isInput = false;
			LogicBullet logicBullet = (LogicBullet)buff.buffParam["LogicBullet"];
			GameMain.InputAKeyEvent += () => { buffTarget.LogicPos = logicBullet.LogicPos; };
		}

		private static void OnNotCanInputBuffEnd(BuffObj buff)
		{
			Debug.Log("玩家打开输入");
			LogicActor buffTarget = buff.target;
			var logicActorState = buffTarget.GetComponent<LogicActorState>();
			logicActorState.state.isInput = true;
			GameMain.InputAKeyEvent = null;
		}

		private static void OnChaDie()
		{
		}

		private static void OnExplosionBuffRemoved(BuffObj buff)
		{
			Debug.Log("玩家 OnExplosionBuffRemoved");
			var buffTarget = buff.target;
			DamageSystem damageSystem = GameObject.FindObjectOfType<DamageSystem>();
			damageSystem.DoDamage(null, buffTarget, new Damage(9999));
			LogicActor actor = (LogicActor)buff.buffParam["NextActor"];
			if (actor == null) return;
			var gameMain = GameObject.FindObjectOfType<GameMain>();
			var nextActor = gameMain.GetNextActor(actor);
			AddExplosionBuff(actor, nextActor);
		}

		/// <summary>
		/// 添加爆炸buff
		/// </summary>
		private static void AddExplosionBuff(LogicActor actor, LogicActor nextActor)
		{
			BuffConfig buffConfig = new BuffConfig()
			{
				onOccur = BuffData.onOccurFunc["OnExplosionBuff"],
				onRemoved = BuffData.onRemovedFunc["OnExplosionBuffRemoved"],
			};
			AddBuffInfo addBuffInfo = new AddBuffInfo(buffConfig, null, actor, 1, 1);
			addBuffInfo.buffParam = new()
			{
				{ "NextActor", nextActor }
			};
			var buffSystem = actor.GetComponent<BuffSystem>();
			buffSystem.AddBuff(addBuffInfo);
		}

		public static void OnTickedFunc(BuffObj buff)
		{
			Debug.Log("OnTickedFunc Test");
		}

		private static void OnCheckBullet(BuffObj buff)
		{
			LogicActor buffTarget = buff.target;
			LogicBullet logicBullet = (LogicBullet)buff.buffParam["LogicBullet"];
			if (logicBullet.isDestroy)
			{
				var buffSystem = buffTarget.GetComponent<BuffSystem>();
				buffSystem.RemoveBuffImmediately("test_buff");
			}
		}

		public static void OnBeHit(BuffObj buff, ref DamageInfo damageInfo, LogicActor attacker)
		{
			Debug.Log("OnBeHit Test");
		}

		public static void OnBeKilled(BuffObj buff, DamageInfo damageInfo, LogicActor attacker)
		{
			Debug.Log("OnBeKilled Test");
		}

		private static void OnActorBeKilled(BuffObj buff, DamageInfo damageInfo, LogicActor attacker)
		{
			LogicActor bindActor = (LogicActor)buff.buffParam["BindActor"];
			var gameMain = GameObject.FindObjectOfType<GameMain>();
			var nextActor = gameMain.GetNextActor(bindActor);
			if (nextActor != null)
			{
				AddExplosionBuff(bindActor, nextActor);
			}
		}
	}
}